﻿using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace JITCompilation
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnSure_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                CodeDomProvider compiler = new CSharpCodeProvider();     //编译器
                CompilerParameters comPara = new CompilerParameters();   //编译器参数




                comPara.GenerateExecutable = false; // 生成exe文件
                comPara.GenerateInMemory = false; // 是否在内存在输出
                //comPara.OutputAssembly = "SimpleCompile.exe"; // 输出文件 




                CompilerResults compilerResults = compiler.CompileAssemblyFromSource(comPara, txbIOCClassCode.Text);
                if (compilerResults.NativeCompilerReturnValue == 0)
                {
                    //if (!Directory.Exists(@"Script"))
                    //{
                    //    Directory.CreateDirectory(@"Script");
                    //}
                    //// 判断设置文件是否存在
                    ////if (!File.Exists(@"Script\" + header +".srcipt"))
                    //{
                    //    File.WriteAllText(@"Script\" + compilerResults.CompiledAssembly.GetType + ".srcipt", requeststring);
                    //}
                    //return (new Response() { Success = true }).ToJsonString();
                }
                else
                {
                    string errors = "";
                    if (compilerResults.Errors.Count > 0)
                    {
                        foreach (CompilerError ce in compilerResults.Errors)
                        {
                            errors += ce.ToString();
                        }
                    }
                    //return (new Response() { Success = false, Message = errors }).ToJsonString();
                }
            }
            catch (Exception ex)
            {
                //return (new Response() { Success = false, Message = ex.ToString() }).ToJsonString();
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txbIOCClassCode.Text= "namespace JITCompilation\r\n{\r\n public class Login\r\n    {\r\n public string Execute(object data)\r\n        {\r\n            return \"登陆\";\r\n        }\r\n    }\r\n}";
        }
    }
}
